-- 王子冈布奥

function initPrinceSlime(self, buildingInfo, dbase, pos)
    if type(dbase) ~= "table" or type(dbase["need_property"]) ~= "table" then
        -- 没要求，配置错误
        error("获取不到王子冈布奥物品配置！");
        return;
    end

    -- 获得王子冈布奥
    local function okFun()
        local bonusArgs = SlimeEventM.query(107, "bonus_args");
        local costList = bonusArgs["cost"];
        local cost;
        local equipType;

        for _, cost in pairs(costList) do
            if type(cost) ~= "table" or cost[1] ~= 1 then
                -- 配置错误
                error("王子冈布奥扣除配置错误！");
                return;
            end

            -- 获取type
            equipType = EquipM.query(cost[2], "type");
            -- 卸下套装
            if not DungeonActionM.go("do_unequip", 1, equipType) then
                -- 卸下失败
                return;
            end
        end

        -- 尝试触发
        DungeonActionM.go("slime_event", pos, 107);

        -- 建筑消失
        playBuildingDisappear(self);
    end

    -- 获得碎片
    local function okFun2()
        local bonusArgs = SlimeEventM.query(108, "bonus_args");
        local costList = bonusArgs["cost"];
        local cost;
        local equipType;

        for _, cost in pairs(costList) do
            if type(cost) ~= "table" or cost[1] ~= 1 then
                -- 配置错误
                error("王子冈布奥扣除配置错误！");
                return;
            end

            -- 获取type
            equipType = EquipM.query(cost[2], "type");
            -- 卸下套装
            if not DungeonActionM.go("do_unequip", 1, equipType) then
                -- 卸下失败
                return;
            end
        end

        -- 尝试触发
        DungeonActionM.go("slime_event", pos, 108);

        -- 建筑消失
        playBuildingDisappear(self);
    end

    local petId = SlimeEventM.query(107, "dbase")["pet_id"];
    -- 建筑图标
    local iconPath = getHeroIconPath(petId);

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 格子失效了
                return;
            end

            if not DungeonBossM.canPickUpTarget() then
                -- 不能解救
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            local equips = dbase["need_property"];
            local equipId;

            -- 套装不齐
            for _, equipId in pairs(equips) do
                if not EquipM.isEquiped(equipId) then
                    -- 没装备
                    self:openBuildingView(nil, 0, buildingInfo["dialog2"], buildingInfo["view_text2"], nil, iconPath, nil, true);
                    AudioM.playFx("slime_gladiator_app");
                    return;
                end
            end

            -- 有王子,显示购买碎片
            if not petId then
                error("SLIME_EVENT_107 (pet_id)没有配置！");
                return;
            end

            if PetM.getMyPet(petId) then
                self:openBuildingView2(okFun2, nil, buildingInfo["view_text"], buildingInfo["view_text2"],
                    buildingInfo["dialog1"], iconPath, true);
            else
                -- 没有王子，显示获得王子
                self:openBuildingView2(okFun, nil, buildingInfo["view_text"], buildingInfo["view_text2"],
                    buildingInfo["dialog1"], iconPath, true);
            end
            AudioM.playFx("slime_gladiator_app");
        end
    end

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;
    self.icon:addTouchEventListener(onClicked);
    self.needAnimation =  true;
    self.icon:setPositionY(self.icon:getPositionY() + 15);

    -- 需要用监狱图标遮挡
    local prisonIcon = getLevelItemIconPath("prison");
    local prisonImg  = ccui.ImageView:create(prisonIcon);
    prisonImg:setTouchEnabled(false);
    prisonImg:setName("prison_icon");
    self.item:addChild(prisonImg);
end
